package com.Jning.cfengtestdemo.controller;

import com.Jning.cfengtestdemo.entity.TestUser;
import com.Jning.cfengtestdemo.service.TestUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Cfeng
 * @date 2022/7/27
 * 只是用于测试
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class TestUserController {

    private final TestUserService userService;

    @PostMapping("/register")
    public String registerUser(@Validated @RequestBody TestUser user) {
        user = userService.registerUser(user);
        if(user == null) {
           return  "注册失败";
        }
        return user.getUserName() + "注册成功";
    }

    @RequestMapping("/queryByName")
    public TestUser queryByName(String userName) {
        System.out.println(userName);
        return userService.findUserByName(userName);
    }

    @GetMapping("/queryAll")
    public List<TestUser> queryAll() {
        return userService.findAll();
    }

}
